<template>
  <el-dialog
    v-model="dialogVisible"
    title="用户登录"
    width="400px"
    :before-close="handleClose"
    class="auth-dialog"
  >
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      label-width="80px"
      label-position="left"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
          clearable
          prefix-icon="User"
        />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="请输入密码"
          clearable
          prefix-icon="Lock"
          show-password
        />
      </el-form-item>

      <el-form-item>
        <div class="form-options">
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
          <el-link type="primary" @click="forgotPassword">忘记密码？</el-link>
        </div>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button
          type="primary"
          :loading="loginLoading"
          @click="handleLogin"
        >
          登录
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
/**
 * 登录对话框组件
 * 
 * 功能：
 * - 用户登录表单
 * - 表单验证
 * - 登录状态管理
 * - 后端接口对接
 */

import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

console.log('LoginDialog组件加载')

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:modelValue', 'login-success'])

// 响应式数据
const loginFormRef = ref()
const loginLoading = ref(false)

const loginForm = ref({
  username: '',
  password: '',
  rememberMe: false
})

// 计算属性
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 表单验证规则（简化版）
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

// 方法定义
const handleClose = () => {
  console.log('关闭登录对话框')
  dialogVisible.value = false
  resetForm()
}

const resetForm = () => {
  loginFormRef.value?.resetFields()
  loginForm.value = {
    username: '',
    password: '',
    rememberMe: false
  }
}

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    
    console.log('提交登录表单:', loginForm.value.username)
    loginLoading.value = true
    
    // 简化登录逻辑：任意用户名和密码都能登录
    await new Promise(resolve => setTimeout(resolve, 800)) // 模拟网络延迟
    
    // 创建模拟用户信息
    const mockUser = {
      id: Date.now(),
      username: loginForm.value.username,
      realName: loginForm.value.username,
      email: `${loginForm.value.username}@example.com`,
      role: '普通用户',
      avatar: '',
      loginTime: new Date().toISOString()
    }
    
    console.log('登录成功，用户信息:', mockUser)
    ElMessage.success(`欢迎登录，${mockUser.realName}！`)
    emit('login-success', mockUser)
    handleClose()
  } catch (error) {
    console.error('表单验证失败:', error)
    ElMessage.error('请检查输入信息')
  } finally {
    loginLoading.value = false
  }
}

const forgotPassword = () => {
  console.log('忘记密码')
  ElMessage.info('忘记密码功能开发中，请联系管理员')
}
</script>

<style scoped>
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 继承父组件的对话框样式 */
:deep(.el-dialog) {
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}

:deep(.el-dialog__header) {
  background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
  color: white;
  padding: 20px 24px;
  border-radius: 12px 12px 0 0;
}

:deep(.el-dialog__title) {
  color: white;
  font-weight: 600;
  font-size: 18px;
}

:deep(.el-dialog__body) {
  padding: 24px;
  background: #fafbfc;
}

:deep(.el-form-item__label) {
  color: #374151;
  font-weight: 600;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  border: 1px solid #d1d5db;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  border: none;
  box-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);
}
</style>
